package com.wjw.service;

import com.wjw.mapper.DamageTypeStableMapper;
import com.wjw.model.Damage_type_stable;
import com.wjw.utils.GetSqlSessionUtils;
import com.wjw.viewmodel.Damage_type_stable_view;
import org.apache.ibatis.session.SqlSession;
import org.springframework.dao.DataAccessException;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 损坏类型子参照表数据库操作:逻辑控制层
 */
@Service
public class DamageTypeStableService {

//    private SqlSession sqlsession= GetSqlSessionUtils.getSqlSession();
//    private DamageTypeStableMapper mapper=sqlsession.getMapper(DamageTypeStableMapper.class);

    public List<Damage_type_stable_view> findAll(){
        SqlSession sqlsession= GetSqlSessionUtils.getSqlSession();
        DamageTypeStableMapper mapper=sqlsession.getMapper(DamageTypeStableMapper.class);
        try {
            List<Damage_type_stable> all = mapper.findAll();
            List<Damage_type_stable_view> a=new ArrayList<Damage_type_stable_view>();
            for(Damage_type_stable e:all){
                Damage_type_stable_view d = new Damage_type_stable_view();
                d.setDamage_type_sid(e.getDamage_type_sid());
                d.setDamage_type_calcu(e.getDamage_type_calcu());
                d.setDamage_type_sname(e.getDamage_type_sname());
                d.setDamage_type_shape(e.getDamage_type_shape());
                d.setDamage_type_fname(mapper.getDamageTypeFtablename(e.getDamage_type_fid()).getDamage_type_fname());
                a.add(d);
            }
            return a;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }finally {
            sqlsession.close();
        }
    }

    public List<Damage_type_stable_view> findByPage(int page,int limit){
        SqlSession sqlsession= GetSqlSessionUtils.getSqlSession();
        DamageTypeStableMapper mapper=sqlsession.getMapper(DamageTypeStableMapper.class);
        try {
            int start=(page-1)*limit;
            List<Damage_type_stable> all = mapper.findByPage(start,limit);
            List<Damage_type_stable_view> a=new ArrayList<Damage_type_stable_view>();
            for(Damage_type_stable e:all){
                Damage_type_stable_view d = new Damage_type_stable_view();
                d.setDamage_type_sid(e.getDamage_type_sid());
                d.setDamage_type_calcu(e.getDamage_type_calcu());
                d.setDamage_type_sname(e.getDamage_type_sname());
                d.setDamage_type_shape(e.getDamage_type_shape());
                d.setDamage_type_fname(mapper.getDamageTypeFtablename(e.getDamage_type_fid()).getDamage_type_fname());
                a.add(d);
            }
            return a;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }finally {
            sqlsession.close();
        }
    }

    public boolean add(Damage_type_stable_view e){
        SqlSession sqlsession= GetSqlSessionUtils.getSqlSession();
        DamageTypeStableMapper mapper=sqlsession.getMapper(DamageTypeStableMapper.class);
        try {
            Damage_type_stable d = new Damage_type_stable();
            d.setDamage_type_sid(e.getDamage_type_sid());
            d.setDamage_type_calcu(e.getDamage_type_calcu());
            d.setDamage_type_sname(e.getDamage_type_sname());
            d.setDamage_type_shape(e.getDamage_type_shape());
            d.setDamage_type_fid(mapper.getDamageTypeFtableId(e.getDamage_type_fname()).getDamage_type_fid());
            mapper.add(d);
            sqlsession.commit();
            return true;
        } catch (Exception e1) {
            e1.printStackTrace();
            return false;
        }finally {
            sqlsession.close();
        }
    }

    public boolean delete(int id){
        SqlSession sqlsession= GetSqlSessionUtils.getSqlSession();
        DamageTypeStableMapper mapper=sqlsession.getMapper(DamageTypeStableMapper.class);
        try {
            mapper.delete(id);
            sqlsession.commit();
            return true;
        } catch (DataAccessException e) {
            e.printStackTrace();
            return false;
        }finally {
            sqlsession.close();
        }
    }

    public boolean change(Damage_type_stable_view e){
        SqlSession sqlsession= GetSqlSessionUtils.getSqlSession();
        DamageTypeStableMapper mapper=sqlsession.getMapper(DamageTypeStableMapper.class);
        try {
            Damage_type_stable d = new Damage_type_stable();
            d.setDamage_type_sid(e.getDamage_type_sid());
            d.setDamage_type_calcu(e.getDamage_type_calcu());
            d.setDamage_type_sname(e.getDamage_type_sname());
            d.setDamage_type_shape(e.getDamage_type_shape());
            d.setDamage_type_fid(mapper.getDamageTypeFtableId(e.getDamage_type_fname()).getDamage_type_fid());
            mapper.change(d);
            sqlsession.commit();
            return true;
        } catch (Exception e1) {
            e1.printStackTrace();
            return false;
        }finally {
            sqlsession.close();
        }
    }

}
